package com.example.bishe;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;


/**
 * 加密
 *
 * @author: LDH
 * @date: 2023年04月08日 20:42
 */
@SpringBootTest
public class jiami {

    public static void main(String[] args) {
        String str = "12345"; // 传入的明文
        String key = "jkdgxvhasjbd345"; // 设置好的key
        String keyBinary = BinaryStringConverteUtil.toBinaryString(key);
        String binary = BinaryStringConverteUtil.toBinaryString(str);
        System.out.println("转换成二进制后:"+binary);

        StringBuffer sb = new StringBuffer(binary);
        sb.insert(0, keyBinary);
        System.out.println("拼接一次:"+sb);
        sb.insert(9+18, keyBinary);
        System.out.println("拼接两次:"+sb);
        sb.insert(18+18 * 2, keyBinary);
        System.out.println("拼接后的二进制字符串:"+sb);

        String s = removeElements(sb.toString());
        System.out.println("隔位去除元素后的字符串:"+s);

        String s1 = reverseString(s);
        System.out.println("逆序后的字符串:"+s1);

        String s2 = BinaryStringConverteUtil.splitToBinary(s1);
        System.out.println("格式化后的二进制字符串:"+s2);

        String s3 = BinaryStringConverteUtil.toString(s2);
        System.out.println("转义后的字符串:"+s3);

    }

    @Test
    public void tt(){
        String s1 = test("aksjh2716317sxcbz");
        String s2 = test("iqowukslahaskhd");
        String s3 = test("12345qq");
        System.out.println("s1:"+s1);
        System.out.println("s2:"+s2);
        System.out.println("s3:"+s3);
//        System.out.println(s1.equals(s2));
    }

    public String test(String str) {
        String binary = BinaryStringConverteUtil.toBinaryString(str);
        StringBuffer sb = new StringBuffer(binary);
        String s = removeElements(sb.toString());
        String s1 = reverseString(s);
        String s2 = BinaryStringConverteUtil.splitToBinary(s1);
        String s3 = BinaryStringConverteUtil.toString(s2);
        return s3;
    }


    // 去除元素
    public static String removeElements(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        int len = str.length();
        if (len <= 1) {
            return str;
        }

        StringBuffer result = new StringBuffer(str);
        int max = 15 * 6;

        for (int i = 0; i < len; i++){
            if (len == max) return result.toString();
            result.deleteCharAt(i);
            len = result.length();
        }

        return removeElements(result.toString());

    }

    // 二进制字符串逆序
    public static String reverseString(String str) {
        return new StringBuilder(str).reverse().toString();
    }


}